package com.thz.system.service.reply.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thz.system.mapper.reply.ReplyMapper;
import com.thz.system.mapper.sysuser.SysUserMapper;
import com.thz.system.mapper.user.UserMapper;
import com.thz.system.pojo.reply.Reply;
import com.thz.system.pojo.reply.vo.ReplyVO;
import com.thz.system.pojo.sysuser.SysUser;
import com.thz.system.pojo.user.User;
import com.thz.system.service.reply.IReplyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author thz
 * @Date 2025/5/11
 */
@Service
public class ReplyServiceImpl implements IReplyService {

    @Autowired
    private ReplyMapper replyMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public Long addReply(Reply reply) {
        replyMapper.insert(reply);
        return reply.getId();
    }

    @Override
    public List<ReplyVO> listByCourseId(Long courseId, Integer pageNum, Integer pageSize) {
        // 分页查一级评论
        Page<Reply> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Reply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Reply::getCourseId, courseId).isNull(Reply::getParentId).orderByAsc(Reply::getId);
        IPage<Reply> rootPage = replyMapper.selectPage(page, wrapper);
        List<ReplyVO> rootVOs = new ArrayList<>();
        for (Reply root : rootPage.getRecords()) {
            ReplyVO vo = toVO(root);
            String author = "";
            User user = userMapper.selectById(root.getCreateUser());
            if (user != null) {
                author = user.getNickName();
                vo.setAuthor(author);
            }
            SysUser sysUser = sysUserMapper.selectById(root.getCreateUser());
            if (sysUser != null) {
                author = sysUser.getNickName();
                vo.setAuthor(author);
            }
            vo.setChildren(getChildren(root.getId(),author));
            vo.setAuthor(author);
            rootVOs.add(vo);
        }
        return rootVOs;
    }

    private List<ReplyVO> getChildren(Long parentId, String author) {
        List<Reply> children = replyMapper.selectList(new LambdaQueryWrapper<Reply>().eq(Reply::getParentId, parentId));
        List<ReplyVO> childVOs = new ArrayList<>();
        for (Reply child : children) {
            ReplyVO vo = toVO(child);
            vo.setChildren(getChildren(child.getId(),author));
            vo.setPostAuthor(author);
            User user = userMapper.selectById(child.getCreateUser());
            if (user != null) {
                vo.setAuthor(user.getNickName());
            }
            SysUser sysUser = sysUserMapper.selectById(child.getCreateUser());
            if (sysUser != null) {
                vo.setAuthor(sysUser.getNickName());
            }
            childVOs.add(vo);
        }
        return childVOs;
    }

    @Override
    public Boolean deleteReply(Long id) {
        deleteRecursively(id);
        return true;
    }

    private void deleteRecursively(Long id) {
        List<Reply> children = replyMapper.selectList(new LambdaQueryWrapper<Reply>().eq(Reply::getParentId, id));
        for (Reply child : children) {
            deleteRecursively(child.getId());
        }
        replyMapper.deleteById(id);
    }

    private ReplyVO toVO(Reply reply) {
        ReplyVO vo = new ReplyVO();
        BeanUtils.copyProperties(reply, vo);
        return vo;
    }
}
